package com.baidu.manager.modules.sys.service.impl;

import com.baidu.manager.common.Constant;
import com.baidu.manager.modules.sys.dao.SysMenuDao;
import com.baidu.manager.modules.sys.entity.SysMenu;
import com.baidu.manager.modules.sys.service.SysMenuService;
import com.baidu.manager.modules.sys.service.SysUserService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.List;
import java.util.Map;

/**
 * @Author: jql
 * @description: TODO(菜单管理业务逻辑层)
 * @Date: Created by 10:39 2018/6/8
 */
@Service
public class SysMenuServiceImp implements SysMenuService {

    @Autowired
    private SysMenuDao sysMenuDao;

    @Autowired
    private SysUserService sysUserService;

    @Override
    public List<SysMenu> queryListByParentId(Long parentId, List<Long> menuIdList) {
        List<SysMenu> menuList = queryListByParentId(parentId);
        if (menuIdList == null){
            return menuList;
        }
        List<SysMenu> userMenuList = new ArrayList<>();
        for (SysMenu menu: menuList){
            if (menuIdList.contains(menu.getId())){
                userMenuList.add(menu);
            }
        }
        return userMenuList;
    }

    @Override
    public List<SysMenu> queryListByParentId(Long parentId) {
        return sysMenuDao.queryListByParentId(parentId);
    }

    @Override
    public List<SysMenu> queryNotButtonList() {
        return sysMenuDao.queryNotButtonList();
    }

    @Override
    public List<SysMenu> getUserMenuList(Long userId) {
        //系统管理员，拥有最高权限
        if (userId == Constant.SUPER_ADMIN){
            return getAllMenuList(null);
        }
        //用户菜单列表
        List<Long> menuIdList = sysUserService.queryAllMenuId(userId);
        return getAllMenuList(menuIdList);
    }

    @Override
    public SysMenu queryObject(Long id) {
        return sysMenuDao.queryObject(id);
    }

    @Override
    public List<SysMenu> queryList(Map<String, Object> map) {
        return sysMenuDao.queryList(map);
    }

    @Override
    public int queryTotal(Map<String, Object> map) {
        return sysMenuDao.queryTotal(map);
    }

    @Override
    public void save(SysMenu menu) {
        sysMenuDao.save(menu);
    }

    @Override
    public void update(SysMenu menu) {
        sysMenuDao.update(menu);
    }

    @Override
    public void deleteBatch(Long[] ids) {
        sysMenuDao.delectBatch(ids);
    }

    @Override
    public List<SysMenu> queryUserList(Long userId) {
        return sysMenuDao.queryList(userId);
    }

    /**
     * 获取所有菜单列表
     * @param menuIdList
     * @return
     */
    private List<SysMenu> getAllMenuList(List<Long> menuIdList){
        //查询根菜单列表
        List<SysMenu> menuList = queryListByParentId(0L,menuIdList);
        //递归获取子菜单
        getMenuTreeList(menuList, menuIdList);

        return menuList;
    }

    /**
     * 递归
     * @param menuList
     * @param menuIdList
     * @return
     */
    private List<SysMenu> getMenuTreeList(List<SysMenu> menuList, List<Long> menuIdList){
        List<SysMenu> subMenuList = new ArrayList<>();
        for (SysMenu sysMenu: menuList){
            if (sysMenu.getType() == Constant.MenuType.CATALOG.getValue()){
                //根目录
                sysMenu.setList(getMenuTreeList(queryListByParentId(sysMenu.getId(),menuIdList),menuIdList));
            }
            subMenuList.add(sysMenu);
        }
        return subMenuList;
    }
}
